/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlysieuthi;

import DAO.CLoai_KhuyenMaiDAO;
import DTO.CSanPham;
import Model.ChiTietKhuyenMai;
import DAO.CSanPhamDAO;
import DAO.SubConfig;
import DTO.CKhuyen_mai;
import DTO.CLoai_KhuyenMai;
import Model.CModel;
import java.awt.event.ItemEvent;
import java.sql.Timestamp;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author quangphuc
 */
public class FrmChiTietKM extends javax.swing.JFrame {

    /**
     * Creates new form FrmChiTietKM
     */
    public FrmChiTietKM() {
        initComponents();
        loadLoaiKhuyenMai();
        loadDataTable();
        count = 0;
    }

    public FrmChiTietKM(FrmKhuyenMai f) {
        initComponents();
        loadLoaiKhuyenMai();
        frmKm = f;
        long km_id = f.m_km_id;
        if (km_id == 1 || km_id == 3) {
            pnlgiam_gia.setVisible(true);
            pnl_san_pham_tang.setVisible(false);
        } else {
            pnlgiam_gia.setVisible(false);
            pnl_san_pham_tang.setVisible(true);
        }
        loadDataTable();
    }
    public FrmChiTietKM(FrmKhuyenMai f,HashMap km) {
        initComponents();        
        frmKm = f;
        loadLoaiKhuyenMai();
        loadDataTable();
        count = 0;
        this.khuyenMai = km;
    }
    List<CSanPham> listSanPham = new ArrayList<CSanPham>();
    CSanPhamDAO spdao = new CSanPhamDAO();
    List<ChiTietKhuyenMai> list_chi_tiet_km = new ArrayList<ChiTietKhuyenMai>();
    FrmKhuyenMai frmKm;
    SubConfig config = null;
    List<CSanPham> lMua1 = new ArrayList<CSanPham>();
    List<CSanPham> lMua2 = new ArrayList<CSanPham>();
    List<CSanPham> lTang1 = new ArrayList<CSanPham>();
    List<CSanPham> lTang2 = new ArrayList<CSanPham>();
    List<CLoai_KhuyenMai> listLKM = new ArrayList<CLoai_KhuyenMai>();
    HashMap parameters = new HashMap();
    HashMap khuyenMai = new HashMap();
    String key_km = "km";
    int count = 0;

    private void setTableValue(List<CSanPham> lsp, JTable table) {
        Vector<Vector> data = new Vector<Vector>();
        table.removeAll();
        for (CSanPham item : lsp) {
            Vector<String> row = new Vector<String>();
            row.addElement(item.getTenSanPham());
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Ten Hang");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table.setModel(model);
        table.setFillsViewportHeight(true);
    }

    private void setTableSoLuong(List<CSanPham> list, JTable tbl) {
        Vector<Vector> data = new Vector<Vector>();
        for (CSanPham item : list) {
            Vector<String> row = new Vector<String>();
            row.addElement(item.getTenSanPham());
            row.addElement("0");
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Tên SP");
        columnNames.addElement("Số lượng");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        tbl.removeAll();
        tbl.setModel(model);
        tbl.setFillsViewportHeight(true);
    }

    private void loadLoaiKhuyenMai() {
        config = SubConfig.getInstance();
        config.init();
        CLoai_KhuyenMaiDAO loaikmDAO = new CLoai_KhuyenMaiDAO();
        this.listLKM = loaikmDAO.getAll(config);
        config.close();
        this.cbbKhuyenmai.removeAllItems();
        for (CLoai_KhuyenMai item : this.listLKM) {
            this.cbbKhuyenmai.addItem(item.getHinhThucKM());
        }
    }

    private void loadDataTable() {
        txtgiam_gia.setText("0");
        config = SubConfig.getInstance();
        config.init();
        listSanPham = spdao.getAll(config);
        lMua1 = listSanPham;
        lTang1 = spdao.getAll(config);
        CLoai_KhuyenMaiDAO loaikmDAO = new CLoai_KhuyenMaiDAO();
        this.listLKM = loaikmDAO.getAll(config);
        config.close();
        this.lMua2 = new ArrayList<CSanPham>();
        this.lTang2 = new ArrayList<CSanPham>();
        this.setTableValue(lMua1, tbl_mua1);
        this.setTableValue(lTang1, tbltang_1);
        this.setTableSoLuong(lMua2, tbl_mua2);
        this.setTableSoLuong(lTang2, tbltang_2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnl_san_pham_tang = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbltang_1 = new javax.swing.JTable();
        btnNext_tang = new javax.swing.JButton();
        btnNext_all_tang = new javax.swing.JButton();
        btnPrev_tang = new javax.swing.JButton();
        btnPrev_all_tang = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbltang_2 = new javax.swing.JTable();
        pnl_san_pham_mua1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbl_mua1 = new javax.swing.JTable();
        btnNext_mua = new javax.swing.JButton();
        btnNext_all_mua = new javax.swing.JButton();
        btnPrev_mua = new javax.swing.JButton();
        btnPrev_all_mua = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbl_mua2 = new javax.swing.JTable();
        btnChapnhan = new javax.swing.JButton();
        pnlgiam_gia = new javax.swing.JPanel();
        txtgiam_gia = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        cbbKhuyenmai = new javax.swing.JComboBox();
        btnClose = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        pnl_san_pham_tang.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn các sản phẩm được tặng"));

        tbltang_1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbltang_1);

        btnNext_tang.setText(">");
        btnNext_tang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNext_tangActionPerformed(evt);
            }
        });

        btnNext_all_tang.setText(">>");
        btnNext_all_tang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNext_all_tangActionPerformed(evt);
            }
        });

        btnPrev_tang.setText("<");
        btnPrev_tang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrev_tangActionPerformed(evt);
            }
        });

        btnPrev_all_tang.setText("<<");
        btnPrev_all_tang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrev_all_tangActionPerformed(evt);
            }
        });

        tbltang_2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tbltang_2);

        javax.swing.GroupLayout pnl_san_pham_tangLayout = new javax.swing.GroupLayout(pnl_san_pham_tang);
        pnl_san_pham_tang.setLayout(pnl_san_pham_tangLayout);
        pnl_san_pham_tangLayout.setHorizontalGroup(
            pnl_san_pham_tangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_san_pham_tangLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnl_san_pham_tangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNext_all_tang)
                    .addComponent(btnNext_tang, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrev_tang, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrev_all_tang))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnl_san_pham_tangLayout.setVerticalGroup(
            pnl_san_pham_tangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_san_pham_tangLayout.createSequentialGroup()
                .addGroup(pnl_san_pham_tangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnl_san_pham_tangLayout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(btnNext_tang)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnNext_all_tang)
                        .addGap(18, 18, 18)
                        .addComponent(btnPrev_tang)
                        .addGap(18, 18, 18)
                        .addComponent(btnPrev_all_tang))
                    .addGroup(pnl_san_pham_tangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addGap(33, 33, 33))
        );

        pnl_san_pham_mua1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn các sản phẩm mua"));

        tbl_mua1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(tbl_mua1);

        btnNext_mua.setText(">");
        btnNext_mua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNext_muaActionPerformed(evt);
            }
        });

        btnNext_all_mua.setText(">>");
        btnNext_all_mua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNext_all_muaActionPerformed(evt);
            }
        });

        btnPrev_mua.setText("<");
        btnPrev_mua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrev_muaActionPerformed(evt);
            }
        });

        btnPrev_all_mua.setText("<<");
        btnPrev_all_mua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrev_all_muaActionPerformed(evt);
            }
        });

        tbl_mua2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(tbl_mua2);

        javax.swing.GroupLayout pnl_san_pham_mua1Layout = new javax.swing.GroupLayout(pnl_san_pham_mua1);
        pnl_san_pham_mua1.setLayout(pnl_san_pham_mua1Layout);
        pnl_san_pham_mua1Layout.setHorizontalGroup(
            pnl_san_pham_mua1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_san_pham_mua1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(pnl_san_pham_mua1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNext_all_mua)
                    .addComponent(btnNext_mua, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrev_mua, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrev_all_mua))
                .addGap(50, 50, 50)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
        pnl_san_pham_mua1Layout.setVerticalGroup(
            pnl_san_pham_mua1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_san_pham_mua1Layout.createSequentialGroup()
                .addGroup(pnl_san_pham_mua1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnl_san_pham_mua1Layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(btnNext_mua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnNext_all_mua)
                        .addGap(18, 18, 18)
                        .addComponent(btnPrev_mua)
                        .addGap(18, 18, 18)
                        .addComponent(btnPrev_all_mua))
                    .addGroup(pnl_san_pham_mua1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addGap(27, 27, 27))
        );

        btnChapnhan.setText("Cập nhật");
        btnChapnhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChapnhanActionPerformed(evt);
            }
        });

        pnlgiam_gia.setBorder(javax.swing.BorderFactory.createTitledBorder("Giảm giá %"));

        txtgiam_gia.setText("0");

        jLabel1.setText("%");

        javax.swing.GroupLayout pnlgiam_giaLayout = new javax.swing.GroupLayout(pnlgiam_gia);
        pnlgiam_gia.setLayout(pnlgiam_giaLayout);
        pnlgiam_giaLayout.setHorizontalGroup(
            pnlgiam_giaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlgiam_giaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtgiam_gia, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlgiam_giaLayout.setVerticalGroup(
            pnlgiam_giaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlgiam_giaLayout.createSequentialGroup()
                .addGroup(pnlgiam_giaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtgiam_gia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn loại khuyến mãi"));

        cbbKhuyenmai.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbKhuyenmai.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbKhuyenmaiItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbbKhuyenmai, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbbKhuyenmai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnClose.setText("Thoát");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnSave.setText("Lưu");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(102, 102, 255));
        jLabel2.setText("QUẢN LÝ CHƯƠNG TRÌNH KHUYẾN MÃI");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnl_san_pham_mua1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlgiam_gia, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pnl_san_pham_tang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnChapnhan)
                .addGap(18, 18, 18)
                .addComponent(btnSave)
                .addGap(18, 18, 18)
                .addComponent(btnClose)
                .addGap(131, 131, 131)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(327, 327, 327))
            .addGroup(layout.createSequentialGroup()
                .addGap(381, 381, 381)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(pnl_san_pham_mua1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(pnl_san_pham_tang, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(93, 93, 93)
                                .addComponent(pnlgiam_gia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnChapnhan)
                            .addComponent(btnSave)
                            .addComponent(btnClose))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnl_san_pham_tang.getAccessibleContext().setAccessibleName("Mat hang de mua");

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnNext_muaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNext_muaActionPerformed
        // TODO add your handling code here:
        int rowIndex = this.tbl_mua1.getSelectedRow();
        if (rowIndex > -1 && rowIndex < this.lMua1.size()) {
            CSanPham item = this.lMua1.get(rowIndex);
            this.lMua2.add(item);
            this.lMua1.remove(rowIndex);
            this.setTableValue(lMua1, tbl_mua1);
            this.setTableSoLuong(lMua2, tbl_mua2);
            //this.setTableValue(lMua2, tbl_mua2);
        }
    }//GEN-LAST:event_btnNext_muaActionPerformed

    private void btnNext_all_muaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNext_all_muaActionPerformed
        // TODO add your handling code here:
        for (CSanPham item : this.lMua1) {
            this.lMua2.add(item);
        }
        this.lMua1.clear();
        this.setTableValue(lMua1, tbl_mua1);
        this.setTableSoLuong(lMua2, tbl_mua2);
    }//GEN-LAST:event_btnNext_all_muaActionPerformed

    private void btnPrev_muaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrev_muaActionPerformed
        // TODO add your handling code here:
        int rowIndex = this.tbl_mua2.getSelectedRow();
        if (rowIndex > -1 && rowIndex < this.lMua2.size()) {
            CSanPham item = this.lMua2.get(rowIndex);
            this.lMua1.add(item);
            this.lMua2.remove(rowIndex);
            this.setTableValue(lMua1, tbl_mua1);
            this.setTableSoLuong(lMua2, tbl_mua2);
        }
    }//GEN-LAST:event_btnPrev_muaActionPerformed

    private void btnPrev_all_muaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrev_all_muaActionPerformed
        // TODO add your handling code here:
        for (CSanPham item : this.lMua2) {
            this.lMua1.add(item);
        }
        this.lMua2.clear();
        this.setTableValue(lMua1, tbl_mua1);
        this.setTableSoLuong(lMua2, tbl_mua2);
    }//GEN-LAST:event_btnPrev_all_muaActionPerformed

    private void btnPrev_tangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrev_tangActionPerformed
        // TODO add your handling code here:
        int rowIndex = this.tbltang_2.getSelectedRow();
        if (rowIndex > -1 && rowIndex < this.lTang2.size()) {
            CSanPham item = this.lTang2.get(rowIndex);
            this.lTang1.add(item);
            this.lTang2.remove(rowIndex);
            this.setTableValue(lTang1, tbltang_1);
            //this.setTableValue(lTang2, tbltang_2);
            this.setTableSoLuong(lTang2, tbltang_2);
        }
    }//GEN-LAST:event_btnPrev_tangActionPerformed

    private void btnPrev_all_tangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrev_all_tangActionPerformed
        // TODO add your handling code here:
        for (CSanPham item : this.lTang2) {
            this.lTang1.add(item);
        }
        this.lTang2.clear();
        this.setTableValue(lTang1, tbltang_1);
        this.setTableSoLuong(lTang2, tbltang_2);
        //this.setTableValue(lTang2, tbltang_2);
    }//GEN-LAST:event_btnPrev_all_tangActionPerformed

    private void btnNext_tangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNext_tangActionPerformed
        // TODO add your handling code here:
        int rowIndex = this.tbltang_1.getSelectedRow();
        if (rowIndex > -1 && rowIndex < this.lTang1.size()) {
            CSanPham item = this.lTang1.get(rowIndex);
            this.lTang2.add(item);
            this.lTang1.remove(rowIndex);
            this.setTableValue(lTang1, tbltang_1);
            this.setTableSoLuong(lTang2, tbltang_2);
            //this.setTableValue(lTang2, tbltang_2);
        }
    }//GEN-LAST:event_btnNext_tangActionPerformed

    private void btnNext_all_tangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNext_all_tangActionPerformed
        // TODO add your handling code here:
        for (CSanPham item : this.lTang1) {
            this.lTang2.add(item);
        }
        this.lTang1.clear();
        this.setTableValue(lTang1, tbltang_1);
        //this.setTableValue(lTang2, tbltang_2);
        this.setTableSoLuong(lTang2, tbltang_2);
    }//GEN-LAST:event_btnNext_all_tangActionPerformed

    
    private void updateSoLuong(List<CSanPham>list,JTable tbl){
        for(int i = 0;i < tbl.getRowCount();i++){
            CSanPham item = list.get(i);
            Object value = tbl.getModel().getValueAt(i, 1);
            System.out.println("value : " + value);
            if(value != null){
                int sl = 0;
                sl = Integer.parseInt(value.toString());
                item.setSoLgTonToiDa(sl);
            }
        }
    }
    private void btnChapnhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChapnhanActionPerformed
        // TODO add your handling code here:
        Double giam_gia = Double.parseDouble(txtgiam_gia.getText());
        if (this.lMua2.size() == 0) {
            JOptionPane.showMessageDialog(this, "Vui lòng chọn sản phẩm khách hàng mua để khuyến mãi");
            return;
        }
        int l = 0;
        ChiTietKhuyenMai ct;// = new ChiTietKhuyenMai();
        int index = this.cbbKhuyenmai.getSelectedIndex();
        int loaikm_id = 1;
        this.list_chi_tiet_km = new ArrayList<ChiTietKhuyenMai>();
        if (index >= 0 && index < listLKM.size()) {
            loaikm_id = (int) listLKM.get(index).getId();
        }
        this.updateSoLuong(this.lMua2, tbl_mua2);        
        this.updateSoLuong(this.lTang2, tbltang_2);        
        if (this.lMua2.size() > this.lTang2.size()) {
            l = this.lTang2.size();
            int i = 0;
            CSanPham item;
            for (i = 0; i < l; i++) {
                ct = new ChiTietKhuyenMai();
                ct.setKMId(loaikm_id);
                item = this.lMua2.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());
                ct.setSoLuongMua(item.getSoLgTonToiDa());
                item = this.lTang2.get(i);
                ct.setMhtId(item.getId());
                ct.setMaHangTang(item.getTenSanPham());
                ct.setGiamGia(giam_gia);
                ct.setSoLuongTang(item.getSoLgTonToiDa());
                if (this.list_chi_tiet_km.contains(ct) == false) {
                    this.list_chi_tiet_km.add(ct);
                }
            }
            while (i < this.lMua2.size()) {
                ct = new ChiTietKhuyenMai();
                item = this.lMua2.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());
                ct.setGiamGia(giam_gia);
                ct.setKMId(loaikm_id);
                ct.setSoLuongMua(item.getSoLgTonToiDa());
                if (this.list_chi_tiet_km.contains(ct) == false) {
                    this.list_chi_tiet_km.add(ct);
                }
                i++;
            }
        } else {
            l = this.lMua2.size();
            int i = 0;
            CSanPham item;
            for (i = 0; i < l; i++) {
                ct = new ChiTietKhuyenMai();
                ct.setKMId(loaikm_id);
                item = this.lMua2.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());
                ct.setSoLuongMua(item.getSoLgTonToiDa());
                item = this.lTang2.get(i);
                ct.setMhtId(item.getId());
                ct.setMaHangTang(item.getTenSanPham());
                ct.setSoLuongTang(item.getSoLgTonToiDa());
                ct.setGiamGia(giam_gia);
                if (this.list_chi_tiet_km.contains(ct) == false) {
                    this.list_chi_tiet_km.add(ct);
                }
            }
            while (i < this.lTang2.size()) {
                ct = new ChiTietKhuyenMai();
                ct.setKMId(loaikm_id);
                item = this.lTang2.get(i);
                ct.setMhmId(item.getId());
                ct.setMaHangMua(item.getTenSanPham());
                ct.setSoLuongTang(item.getSoLgTonToiDa());
                ct.setGiamGia(giam_gia);
                if (this.list_chi_tiet_km.contains(ct) == false) {
                    this.list_chi_tiet_km.add(ct);
                }
                i++;
            }
        }
        //parameters = new HashMap();
        key_km = "km";
        count++;
        String _key = key_km + String.format("%s", count);
        parameters.put(_key, this.list_chi_tiet_km);
        this.loadDataTable();
        
        //this.frmKm.setChiTietKm(list_chi_tiet_km);
        //this.setVisible(false);
    }//GEN-LAST:event_btnChapnhanActionPerformed

    private void cbbKhuyenmaiItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbKhuyenmaiItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            int index = cbbKhuyenmai.getSelectedIndex();
            if (index == 0 || index == 2) {
                pnl_san_pham_tang.setVisible(false);
                pnlgiam_gia.setVisible(true);
            } else {
                pnl_san_pham_tang.setVisible(true);
                pnlgiam_gia.setVisible(false);
            }
        }
    }//GEN-LAST:event_cbbKhuyenmaiItemStateChanged

    private void saveKhuyenMai() {
        String tenct = khuyenMai.get("tenct").toString();
        Date d = (Date) khuyenMai.get("tungay");
        Date d1 = (Date) khuyenMai.get("denngay");
        String mota = khuyenMai.get("mota").toString();
        Timestamp tu_ngay = new Timestamp(d.getYear(), d.getMonth(), d.getDate(), 0, 0, 5, 0);
        Timestamp den_ngay = new Timestamp(d1.getYear(), d1.getMonth(), d1.getDate(), 23, 59, 40, 0);
        List<CKhuyen_mai> list_khuyen_mai = new ArrayList<CKhuyen_mai>();
        
        for (int i = 0; i < this.parameters.size(); i++) {
            String key = this.key_km + String.format("%s", i + 1);
            List<ChiTietKhuyenMai> lctkm = (List<ChiTietKhuyenMai>) this.parameters.get(key);
            String mat_hang_mua = "";
            String mat_hang_tang = "";
            double giam_gia = 0.0;
            int loai_kmid = 1;
            for (ChiTietKhuyenMai item : lctkm) {
                String strMua = "";
                String strTang = "";
                loai_kmid = item.getKmId();
                if (item.getMhmId() != -1) {
                    strMua = "" + item.getMhmId() + "::" + item.getSoLuongMua();
                    if (mat_hang_mua.equals("") == false) {
                        mat_hang_mua = mat_hang_mua + ",";
                    }
                    mat_hang_mua = mat_hang_mua + strMua;
                }
                if (item.getMhtId() != -1) {
                    strTang = "" + item.getMhtId() + "::" + item.getSoLuongTang();
                    if (mat_hang_tang.equals("") == false) {
                        mat_hang_tang = mat_hang_tang + ",";
                    }
                    mat_hang_tang = mat_hang_tang + strTang;
                }
                giam_gia = item.getGiamgia();
            }
            CKhuyen_mai km_item = new CKhuyen_mai();
            km_item.setLoaiKm(tenct);
            km_item.setApDung(true);
            km_item.setDenNgay(den_ngay);
            km_item.setGiamGia(giam_gia);
            km_item.setId(1);
            km_item.setKmId(loai_kmid);
            km_item.setLoaiKH_AD(true);
            km_item.setMatHangMua(mat_hang_mua);
            km_item.setMatHangTang(mat_hang_tang);
            km_item.setMoTa(mota);
            km_item.setNguoiTao(QuanLySieuThi.user_name);
            km_item.setTuNgay(tu_ngay);
            list_khuyen_mai.add(km_item);
        }
        CModel _model = new CModel(QuanLySieuThi.user_name);
        _model.insertKhuyenMai(list_khuyen_mai);
        _model.saveHisTory();
        _model.Close();
    }

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        this.saveKhuyenMai();
        this.frmKm.reLoadData();
        this.setVisible(false);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnCloseActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmChiTietKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmChiTietKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmChiTietKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmChiTietKM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmChiTietKM().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChapnhan;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnNext_all_mua;
    private javax.swing.JButton btnNext_all_tang;
    private javax.swing.JButton btnNext_mua;
    private javax.swing.JButton btnNext_tang;
    private javax.swing.JButton btnPrev_all_mua;
    private javax.swing.JButton btnPrev_all_tang;
    private javax.swing.JButton btnPrev_mua;
    private javax.swing.JButton btnPrev_tang;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbKhuyenmai;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPanel pnl_san_pham_mua1;
    private javax.swing.JPanel pnl_san_pham_tang;
    private javax.swing.JPanel pnlgiam_gia;
    private javax.swing.JTable tbl_mua1;
    private javax.swing.JTable tbl_mua2;
    private javax.swing.JTable tbltang_1;
    private javax.swing.JTable tbltang_2;
    private javax.swing.JTextField txtgiam_gia;
    // End of variables declaration//GEN-END:variables
}
